package ma.co.ba.BusinessApplications.presentation.controllers.prestations;

import java.util.List;

import ma.co.ba.BusinessApplications.business.commons.CommonBusiness;
import ma.co.ba.BusinessApplications.business.prestations.PrestationBusiness;
import ma.co.ba.BusinessApplications.business.utilisateurs.UtilisateurBusiness;
import ma.co.ba.BusinessApplications.model.Prestation;
import ma.co.ba.BusinessApplications.model.Utilisateur;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/prestations")
public class PrestationController {
	@Autowired
	UtilisateurBusiness utilisateurBusiness;
	
	@Autowired
	CommonBusiness commonBusiness;

	@Autowired
	PrestationBusiness prestationBusiness;

	@RequestMapping(method = RequestMethod.GET)
	public String prestationsPage(ModelMap model,
			@ModelAttribute("newPrestation") Prestation newPrestation) {

		return "prestations";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deletePrestation(@RequestParam("id") int prestationId) {

		Prestation prst = prestationBusiness.getPrestationById(prestationId);
		commonBusiness.deleteEntity(prst);

		return "redirect:/prestations";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(
			@ModelAttribute("newPrestation") Prestation newPrestation,
			BindingResult result, SessionStatus status) {

		commonBusiness.saveEntity(newPrestation);
		return "redirect:/prestations";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String getUpdateFamilyPrestationModal(ModelMap model,
			@ModelAttribute("id") int prestId) {

		Prestation upPrestation = prestationBusiness.getPrestationById(prestId);
		
		model.addAttribute("upPrestation",upPrestation);

		return "editPrestation";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateFamilyProductModal(ModelMap model,
			@ModelAttribute("upPrestation") Prestation upPrestation) {

		//System.out.println(upPrestation.getId());
		commonBusiness.updateEntity(upPrestation);

		return "redirect:/prestations";
	}

	
	@ModelAttribute("newPrestation")
	Prestation getNewPrestation() {
		return new Prestation();
	}

	@ModelAttribute("prestationsList")
	List<Prestation> getPrestationsList() {
		return prestationBusiness.getAllPrestations();
	}

	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
}
